/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("Mechanica",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Demo.IServices;
using VOL.Demo.IRepositories;
using Microsoft.EntityFrameworkCore;
using VOL.Core.Filters;
using VOL.Core.Enums;
using VOL.Core.Utilities;
using OfficeOpenXml;
using System.Net.Mime;
using Sys = System.IO;
using Dapper;
using VOL.Core.DBManager;
using System.Linq;
using VOL.Core.Infrastructure;
using VOL.Entity.DomainModels.Core;

namespace VOL.Demo.Controllers
{
    public partial class MechanicaController
    {
        private readonly IMechanicaService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IMechanica_records2Service _mechanica_Records2Service;
        private readonly IMechanica_records2Repository _mechanica_Records2Repository;
        [ActivatorUtilitiesConstructor]
        public MechanicaController(
            IMechanicaService service,
            IHttpContextAccessor httpContextAccessor,
            IMechanica_records2Service mechanica_Records2Service,
            IMechanica_records2Repository mechanica_Records2Repository

            


        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
            _mechanica_Records2Service = mechanica_Records2Service;
            _mechanica_Records2Repository = mechanica_Records2Repository;

        }
        [Route("getMechanica_Records2"), HttpPost]
        public IActionResult GetMechanica_Records2([FromBody] PageDataOptions loadDate)
        {
            var gridDate = _mechanica_Records2Service.GetPageData(loadDate);
            return JsonNormal(gridDate);

        }
        [Route("getMechanica_Records2Rows"), HttpGet]

        public async Task<IActionResult> GetMechanica_Records2Rows(int Id)

        {
            var rows = await _mechanica_Records2Repository.FindAsIQueryable(x => x.id == Id).ToListAsync();

            return JsonNormal(rows);
        }
        [HttpGet, Route("print")]
        [ApiActionPermission("Mechanica", ActionPermissionOptions.Print)]
        public IActionResult Print()
        {
            return Content("打印的内容");
        }

        /// <summary>
        /// 导出EXCEL1
        /// </summary>
        /// <param name="wheres"></param>
        /// <returns></returns>
        [HttpPost,Route("testExport")]
        public IActionResult TestExport([FromBody] List<SearchParameters> wheres)
        {
            List<Dictionary<string, Object>> rows = new List<Dictionary<string, object>>();
            rows.Add(new Dictionary<string, object>() { { "测试1","123"}, { "测试2", "345" } });
            rows.Add(new Dictionary<string, object>() { { "测试1", "1233" }, { "测试2", "2414" } });

            string path = EPPlusHelper.ExportGeneralExcel(rows, "test.xlsx", onFillCell: (ExcelWorksheet worksheet, int rowIndex, int columnIndex, object value) =>
            {

            });
            return File(Sys.File.ReadAllBytes(path), MediaTypeNames.Application.Octet, "text.xlsx");
        }

        /// <summary>
        /// 导出EXCEL2
        /// </summary>
        /// <param name="wheres"></param>
        /// <returns></returns>
        [HttpPost, Route("test1Export")]
        public IActionResult Test1Export([FromBody] List<SearchParameters> wheres)
        {
            string sql = "select sbmc as 设备名称 ,sbbm as 设备编码 ,sbxh as 设备型号,sbtp as 设备图片  from mechanica where 1=1";
            DynamicParameters dynamicParameters = new DynamicParameters();
            wheres.ForEach(item =>
            {
                if (item.Name == "sbxh" && !string.IsNullOrEmpty(item.Value))
                {
                    sql += "and sbxh=@sbxh";
                    dynamicParameters.Add("sbxh", item.Value);
                }
            });

            var rows = DBServerProvider.SqlDapper.QueryList<object>(sql, dynamicParameters).Select(item => item as IDictionary<string, object>).ToList();


            //通过字典转换
            //var dicList = DictionaryManager.GetDictionary("设备类型2").Sys_DictionaryList;
            //foreach (var item in rows)
            //{
            //    string value = item["设备型号"].ToString();
            //    item["设备型号"] = dicList.Where(x => x.DicValue == value).Select(x => x.DicName).FirstOrDefault();
            //}


            string path = EPPlusHelper.ExportGeneralExcel(rows, "test.xlsx", onFillCell: (ExcelWorksheet worksheet, int rowIndex, int columnIndex, object value) =>
            {

            });
            return File(Sys.File.ReadAllBytes(path), MediaTypeNames.Application.Octet, "text.xlsx");
        }
    }
}
